package com.class05;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/servlet1")
public class Servlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
//        1. 获取session对象
        HttpSession session = req.getSession();
//        1.1设置session的默认存活时间 20s
        session.setMaxInactiveInterval(20);
//        2. 获取session的id值
        String id = session.getId();
        System.out.println(id);
        boolean aNew = session.isNew();
        System.out.println(aNew);
//        3、setAttribute设置session的值
        session.setAttribute("name","yxy");
        if (id != null){
            System.out.println("已创建新的session");
        }else {
            System.out.println("未创建新的session");
        }
//        设置响应头和编码格式
        resp.setContentType("text/html;charset=utf-8");
//        设置响应体字符集编码格式
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().write("ok");



    }
}
